package com.ybkj.chat.module.dynamic.activity;


import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import com.ybkj.chat.R;
import com.ybkj.chat.base.BaseActivity;
import com.ybkj.chat.module.dynamic.fragment.AddGroupFragment;
import com.ybkj.chat.module.dynamic.fragment.AddpeopleFragment;
import com.ybkj.chat.ui.view.TabLayoutHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 添加好友1 总页面
 */
public class AddFriendActivity extends BaseActivity {
    @BindView(R.id.commom_rl)
    RelativeLayout commomRl;
    @BindView(R.id.tl_tablayout)
    TabLayout mTlTablayout;
    @BindView(R.id.frame)
    FrameLayout frame;

    private AddGroupFragment addGroupFragment;
    private AddpeopleFragment addpeopleFragment;

    //当前显示的fragment
    private Fragment mCurrentFragment;
    private List<Fragment> mFragmentList = new ArrayList<>();

    @Override
    protected void initTitle() {
        setTitleText("添加好友");
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_add_friend;
    }

    @Override
    protected void initView() {
        initTab();
        initFragment();
    }

    @Override
    protected void initData() {

    }

    private void initTab() {
        mTlTablayout.addTab(mTlTablayout.newTab().setText("找人"));
        mTlTablayout.addTab(mTlTablayout.newTab().setText("找群"));

        mTlTablayout.post(new Runnable() {
            @Override
            public void run() {
                TabLayoutHelper.setTabLine(mTlTablayout, 60, 60, mContext);
            }
        });

        mTlTablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                convertTab(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

    }

    private void initFragment() {
        if (addpeopleFragment == null) {
            addpeopleFragment = new AddpeopleFragment();
        }
        if (addGroupFragment == null) {
            addGroupFragment = new AddGroupFragment();
        }

        mFragmentList.add(addpeopleFragment);
        mFragmentList.add(addGroupFragment);

        mCurrentFragment = addpeopleFragment;

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.frame, mCurrentFragment).commit();

    }

    private void convertTab(int position) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (!mFragmentList.get(position).isAdded()) {
            transaction.hide(mCurrentFragment).add(R.id.frame, mFragmentList.get(position));
        } else {
            transaction.hide(mCurrentFragment).show(mFragmentList.get(position));
        }
        mCurrentFragment = mFragmentList.get(position);
        transaction.commit();
    }

}
